<?php
namespace control;
/*
*待考试类
*/

class beTestedClass extends adIndexClass
{
    private const page = 'beTested';
    private const title = '待考试';

    /**
     * 字段初始化
     */
    public function fieldInit($fun): array
    {
        switch ($fun) {
            default:
                $tr = [
                    'name'          => [
                        'name'  => L('person.table.sjbt'),
                        'width' => 200,
                    ],
                    'topicNum'  => [
                        'name'  => L('person.table.ti_mu_zong_shu'),
                        'width' => 200,
                    ],
                    'score'         => [
                        'name'  => L('person.table.zong_fen'),
                        'width' => 200,
                    ],
                    'passMark'          => [
                        'name'  => L('person.table.he_ge_fen_zhi'),
                        'width' => 200,
                        'sort' => 1,
                    ],
                    'startTime'          => [
                        'name'  => L('person.table.kskssj'),
                        'width' => 200,
                        'sort' => 1,
                    ],
                    'endTime'          => [
                        'name'  => L('person.table.ksjssj'),
                        'width' => 200,
                        'sort' => 1,
                    ],
                    'state'          => [
                        'name'  => L('person.table.kszt'),
                        'width' => 150,
                    ],
                    'mx'          => [
                        'name'  => L('person.table.cao_zuo'),
                        'width' => 150,
                    ]
                ];
        }
        return $tr;
    }

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    L('person.table.sjbt') => text(array("name" => "title")),
                    L('person.table.kskssj')   => text(array("name" => "createTime", 'laydate' => ['range' => '~'])),
                    L('person.table.zhuang_tai') => select(array("name" => "state", "option" => ['未开始','进行中','已结束'])),
                ]
            ]
        ],1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /*列表-查询条件*/
    public function home()
    {
        $html = $this->homeOptions(false);
        $button = "";
        //返回
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /*客户列表-查询结果*/
    public function homeSearch()
    {
        //like查询数组
        $like = array(
            "b.title" => $this->post('title'),//试卷标题
        );
        //全等查询数组
        $equal = array(
            "b.state" => '已发布',//状态
        );
        //过滤掉已交卷的试卷
        $and = " and a.stid = '{$this->control['stid']}'  and a.stid not in (select stid from topic_answer_record where paperId = b.id and isCommit = 1)";
        if ($this->post('createTime')) {
            $and .=$this->timeComparison($this->post('createTime'), field: 'startTime');
        }
        $and .= match ($this->post('state')) {
            '未开始' => "  and UNIX_TIMESTAMP(startTime) > UNIX_TIMESTAMP()",
            '进行中' => "  and UNIX_TIMESTAMP(startTime) <= UNIX_TIMESTAMP() and UNIX_TIMESTAMP(endTime) >= UNIX_TIMESTAMP()",
            '已结束' => " and UNIX_TIMESTAMP(b.endTime) < UNIX_TIMESTAMP()",
            default=>'',
        };
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'a.time',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        //获取查询结果
        $where = search(array("like" => $like, "=" => $equal)) . $and;
        $page = $this->page(array(
            "table" =>" paper_people as a join paper_library as b on a.orderId = b.id",
            'field'=>"b.*,a.stid as staffId ",
            "where" => $where,
            "order" => $orderBy['orderBy']
        ));
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array();
        //获取记录
        foreach ($page['array'] as $array) {
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'topicNum':
                        $topicNum = static::total(array('table'=>'paper_library_detail','where'=>" and orderId = '$array[id]'"));
                        $row[] = $topicNum;
                        break;
                    case 'score':
                        $score = find(array('table'=>'paper_library_detail','sum'=>"score",'where'=>" and orderId = '$array[id]'"));
                        $row[] = $score;
                        break;
                    case 'state':
                        if (time() >= strtotime($array['startTime']) && time() <= strtotime($array['endTime'])) {
                            $state = '进行中';
                        }elseif(time() > strtotime($array['endTime'])){
                            $state = '已结束';
                        } else {
                            $state = '未开始';
                        }
                        $row[] = $state;
                        break;
                    case 'mx':
                        if (strtotime($array['endTime'])<time()){
                            $mx = "<span style='color: #e6e6e6'>".L('person.table.startTest')."</span>";
                        }else{
                            $mx = "<span class='del-row-button' onclick=\"window.open('" . root . "control/adIndex.html?cla=paperManagement&fun=previewPaper&method=answer&id={$array['id']}')\">".L('person.table.startTest')."</span>";
                        }
                        $row[] = $mx;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        //控件
        $button = "";
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
        ));
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='listForm'>" . $li . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }

}

